package model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Account implements Serializable {

	private String AccountId;  //学号
	private String AccountName;  //姓名
	private Double AccountBalance;  //余额
	private Double IFUtilityBill;//水电费是否缴纳(非0则表示未缴纳)
	private Double IFAccommodationFee;
	private Double IFTution;
	private String utilityDeadline;
	private String accomDeadline;
	private String tutionDeadline;
	public ArrayList<JournalAccount>Accountlist;//流水

	public int getBillsNum()
	{
		int i=0;
		if(IFUtilityBill>0) ++i;
		if(IFAccommodationFee>0) ++i;
		if(IFTution>0) ++i;
		return i;

	}
	public Account(String accountId) {
		AccountId = accountId;
		AccountBalance = 0.0;
	}

	/**
	 * 支付水电费、学费等
	 * @param type 1、2、3分别表示水电费、住宿费、学费
	 * @return 是否支付成功
	 */
	public boolean pay(String type){
		int i = Integer.parseInt(type);
		switch (i) {
			case 1:
				if(IFUtilityBill > 0.0){
					//需要缴纳水电费。余额大于水电费才能缴纳成功
					if(AccountBalance >= IFUtilityBill){
						AccountBalance -= IFUtilityBill;
						String time = getNowTime();
						JournalAccount journalAccount = new JournalAccount("水电费",IFUtilityBill,time,AccountId);
						Accountlist.add(journalAccount);
						IFUtilityBill = 0.0;
						return true;
					}else{
						return false;
					}
				}else{
					//无需缴费
					return false;
				}
			case 2:
				if(IFAccommodationFee > 0.0){
					//需要缴纳住宿费。余额大于费用才能缴纳成功
					if(AccountBalance >= IFAccommodationFee){
						AccountBalance -= IFAccommodationFee;
						String time = getNowTime();
						JournalAccount journalAccount = new JournalAccount("住宿费",IFAccommodationFee,time,AccountId);
						Accountlist.add(journalAccount);
						IFAccommodationFee = 0.0;
						return true;
					}else{
						return false;
					}
				}else{
					//无需缴费
					return false;
				}
			case 3:
				if(IFTution > 0.0){
					//需要缴纳学费。余额大于费用才能缴纳成功
					if(AccountBalance >= IFTution){
						AccountBalance -= IFTution;
						String time = getNowTime();
						JournalAccount journalAccount = new JournalAccount("学费",IFTution,time,AccountId);
						Accountlist.add(journalAccount);
						IFTution = 0.0;
						return true;
					}else{
						return false;
					}
				}else{
					//无需缴费
					return false;
				}
			default:

				return false;
		}
	}

	/**
	 * 其他类别的费用
	 * @param money 金额
	 * @return
	 */
	public boolean pay(String item,Double money){
		if(money > 0.0 && money <= AccountBalance){
			//金额大于0且不高于余额
			AccountBalance -= money;
			String time = getNowTime();
			JournalAccount journalAccount = new JournalAccount(item,money,time,AccountId);
			Accountlist.add(journalAccount);
			return true;
		}
		return false;
	}


	/**
	 * 充值
	 * @param money 充值的金额
	 * @return 是否充值成功
	 */
	public boolean recharge(Double money,String item){
		if(money > 0.0){
			//充值金额要大于0
			AccountBalance += money;
			String time = getNowTime();
			JournalAccount journalAccount = new JournalAccount(item,money,time,AccountId,"true");
			Accountlist.add(journalAccount);
			return true;
		}else {
			return false;
		}
	}

	private String getNowTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		Date d = new Date();
		return sdf.format(d);
	}
	public String toType(String cW) {
		switch(cW)
		{
			case "水电费":cW="1";break;
			case "住宿费":cW="2";break;
			case "学费":cW="3";break;
		}
		return cW;
	}

}
